#include <stdio.h>
#include <string.h>
#define NDEBUG
#include <assert.h>

#define DIMENSION 80
#define QSIZE 800
#define enqueue(x,y) queue[qrear][0]=(x);queue[qrear][1]=(y);qrear=(qrear+1)%QSIZE

char board[DIMENSION+2][DIMENSION+2],depth[DIMENSION+2][DIMENSION+2];
int step[4][2] = {{-1,0},{1,0},{0,-1},{0,1}};
short queue[QSIZE][2], qhead, qrear;
int width,height;

int bfs(int startx, int starty, int endx, int endy) {
	int x,y;
	int i,newDepth;
	depth[starty][startx]=0;
	enqueue(startx,starty);
	while (qhead!=qrear) {
		x=queue[qhead][0];
		y=queue[qhead][1];
		newDepth = depth[y][x]+1;
		for (i=0; i<4; i++) {
			for (x=queue[qhead][0]+step[i][0], y=queue[qhead][1]+step[i][1]; 
				 0<=x && x<=width+1 && 0<=y && y<=height+1 && board[y][x]!='X';
				 x+=step[i][0], y+=step[i][1]) {
				if (endx==x&&endy==y) return newDepth;
				//else if (board[y][x] == 'X') break;
				else if(depth[y][x]==0){
					depth[y][x]=newDepth;
					enqueue(x,y);
					assert((qrear+1)%QSIZE!=qhead);
				}
			}
		}
		qhead = (qhead+1)%QSIZE;
	}
	return 0;
}

int main (int argc, const char * argv[]) {
    int dataset,pairset;
	int x1,x2,y1,y2;
	int i,ans;
	freopen("/Users/chenxu/workspace/1101/1101.in", "r", stdin);
	for (dataset=1; ; dataset++) {
		scanf("%d %d",&width,&height);
		getchar();
		if(!width) break;
		printf("Board #%d:\n",dataset);
		memset(board,0,sizeof(board));
		for (i=1; i<=height; i++) fgets(&board[i][1], DIMENSION+1, stdin);
		for (pairset=1; ; pairset++) {
			scanf("%d %d %d %d",&x1,&y1,&x2,&y2);
			if (!x1) break;
			printf("Pair %d: ",pairset);
			memset(depth,0,sizeof(depth));
			qhead=qrear=0;
			board[y2][x2]=0;
			ans = bfs(x1, y1, x2, y2);
			board[y2][x2]='X';
			if (ans>0) printf("%d segments.\n",ans);
			else printf("impossible.\n");
		}
		printf("\n");
	}
    return 0;
}
